<?php

class CreatixPDF extends mPDF
{
	protected $filename;
	protected $img_dir;
	public	  $local_file;
	protected $txt_cabecalho_aux;
	/*
	 * margens do corpo do PDF. Devem ser setadas em mm.
	 */
	protected $margin_l;
	protected $margin_r;
	protected $margin_t;
	protected $margin_b;
	protected $margin_h;
	protected $margin_f;

	protected $tp_data_header;
	protected $my_tp_header;
	public 	  $preview;



	public function __construct($filename = '')
	{

		parent::__construct();

		$this->filename 			= ($filename != "") ? strtolower($filename).".pdf" : "";

		$this->img_dir				= sfConfig::get('sf_web_dir').'\\images\\';
		$this->useOnlyCoreFonts 	= true;//liberando acesso à fonte times, setada no mpdf.css
		$this->setAutoTopMargin		= 'stretch';//margin do header em relacao ao texto

		/*
		 * Definindo padrões ao sistema. Para os que não seguirem as mesmas normas, basta alterar na classe especifica
		 */
		$this->txt_cabecalho_aux	= '';
		$this->margin_l				= 10;
		$this->margin_r				= 10;
		$this->margin_t				= 15;
		$this->margin_b				= 30;
		$this->margin_h				= 15;
		$this->margin_f				= 15;
		//$this->SetBasePath('file://www.meusite.com/');

		//$this->showImageErrors 		= true;

	}

	//Retorna as margens defaults dos documentos.
	public static function getDefaultMargins($medida	= 'mm', $index = null )
	{
		$array_margin	= array(
								"nu_margem_esquerda" 	=> 	30,
								"nu_margem_direita"		=>	15,
								"nu_margem_topo"		=>	15,
								"nu_margem_base"		=>	30,
								"nu_margem_cabecalho"	=>	15,
								"nu_margem_rodape"		=>	15
								);

		if($medida == 'cm')
		{
			foreach($array_margin as $chave => $margem)
				$array_margin[$chave]	= str_replace(".", ",", $margem/10);
		}

		if(!is_null($index))
			return $array_margin['nu_margem_'.$index];
		else
			return $array_margin;
	}

	protected function getHeader($tp_header = null, $dynamic = null)
	{
		if($tp_header)
		{
			if(count($this->my_tp_header))
			{
				foreach($this->my_tp_header as $k => $v)
					$this->tp_data_header[$k] = $v;
			}

			$key			= array("FNDE");

			if(array_key_exists($tp_header, $this->tp_data_header))
			{
				$key = array_keys($this->tp_data_header[$tp_header]);

			}

			$method_header	= "cabecalho".$key[0];

			$this->$method_header($this->tp_data_header[$tp_header][$key[0]]);
		}
		else
		{
			$this->SetHTMLHeader($dynamic);
		}
	}

	public function gerarPDF($content, $rodape = null, $orientation = null, $output = 'D')
	{
		$this->getHeader($tp_header, $dynamicHeader);

		//$this->rodape($rodape);

		$this->AddPage($orientation, '', '', '', '', $this->margin_l, $this->margin_r, $this->margin_t, $this->margin_b, $this->margin_h, $this->margin_f); //margin-left, margin-right, margin-top, margin-bottom, margin-header; EM milimetros

		$this->cabecalhoSecundario($this->txt_cabecalho_aux, false, $dynamicHeader);

		$this->WriteHTML($content);
    	$this->Output($this->filename, $output);

    	if($output == 'D')
    		exit();
	}

	protected function cabecalhoSecundario($texto = null, $hasNumberPage = false, $header_full = null)
	{
		$cabecalho = '<table style="font-size:10px;"><tr>';
		$cabecalho .= '<td>'.utf8_encode($texto).'</td>';

		if($hasNumberPage)
			$cabecalho .= '<td> {PAGENO} de {nb} </td>';

		$cabecalho .= '</tr></table>';

		if($header_full)
			return $this->getHeader($header_full);

		$this->SetHTMLHeader($cabecalho);
	}
}

?>